package media

import (
	"errors"
	"server/common"

	"github.com/jiyeyuran/mediasoup-go/v2"
	"github.com/mitchellh/mapstructure"
)

// -------------------------------------------------------------------------------------------------
//
// -------------------------------------------------------------------------------------------------
func (s *MediaService) checkCreateTransportReqData(data common.CreateTransportReqData) error {
	// Check if router ID is provided
	if data.RouterId == "" {
		return errors.New("router ID is required in create WebRTC transport request")
	}

	// Check if producing or consuming flags are set
	if !data.Producing && !data.Consuming {
		return errors.New("at least one of producing or consuming must be true")
	}

	// Check if transport type is provided
	if data.TransportType == "" {
		return errors.New("transport type is required in create WebRTC transport request")
	}

	// Only support WebRTC transport for now
	if data.TransportType != common.WEBRTC_TRANSPORT {
		return errors.New("only WebRTC transport is supported in create WebRTC transport request")
	}

	return nil
}

// -------------------------------------------------------------------------------------------------
//
// -------------------------------------------------------------------------------------------------
func (s *MediaService) sendCreateTransportResponse(rid string, code int, msg string,
	data common.CreateTransportReqData, transport *mediasoup.Transport) {

	var transportData *mediasoup.WebRtcTransportData
	if transport != nil {
		transportData = transport.Data().WebRtcTransportData
	}

	res := common.Message{
		Cmd: common.CREATE_TRANSPORT_RES,
		Rid: rid,
		Data: common.ResMsgData{
			Code: code,
			Msg:  msg,
			Data: common.CreateTransportResData{
				TransportId:    transport.Id(),
				IceParameters:  &transportData.IceParameters,
				IceCandidates:  transportData.IceCandidates,
				DtlsParameters: &transportData.DtlsParameters,
				SctpParameters: transportData.SctpParameters,
			},
		},
	}

	if err := s.wsClient.Send(res); err != nil {
		LOG(rid).WithError(err).Error("failed to send create transport response")
	}
}

// -------------------------------------------------------------------------------------------------
//
// -------------------------------------------------------------------------------------------------
func (s *MediaService) handleCreateTransportReq(msg common.Message) {
	LOG(msg.Rid).WithField("msg", common.ToJson(msg)).
		Info("handling create WebRTC transport request")

	var reqData common.CreateTransportReqData
	if err := mapstructure.Decode(msg.Data, &reqData); err != nil {
		LOG(msg.Rid).WithError(err).Error("failed to decode create WebRTC transport request data")
		s.sendCreateTransportResponse(msg.Rid, 1, err.Error(), reqData, nil)
		return
	}

	if err := s.checkCreateTransportReqData(reqData); err != nil {
		LOG(msg.Rid).WithError(err).Error("invalid create WebRTC transport request data")
		s.sendCreateTransportResponse(msg.Rid, 1, err.Error(), reqData, nil)
		return
	}

	transport, err := s.CreateTransport(msg.Rid, reqData)
	if err != nil {
		LOG(msg.Rid).WithError(err).Error("failed to create WebRTC transport")
		s.sendCreateTransportResponse(msg.Rid, 1, err.Error(), reqData, nil)
		return
	}

	s.sendCreateTransportResponse(msg.Rid, 0, "success", reqData, transport)
}
